package com.coocaa.aries.jmeter;

import com.coocaa.aries.core.base.model.R;
import com.coocaa.aries.core.tool.json.JsonUtil;
import com.coocaa.aries.jmeter.entity.ExtTestCaseInfoEntity;
import com.coocaa.aries.jmeter.model.JmxUploadResult;
import com.coocaa.aries.jmeter.model.query.ExtTestCaseQueryModel;
import com.coocaa.aries.jmeter.service.IExtTestCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author : bijiahao
 * @date : 2019/4/9.
 * @description: Jmeter测试用例 控制器
 */
@RestController
@RequestMapping("/testCase")
public class ExtTestCaseInfoController {
    @Autowired
    private IExtTestCaseService extTestCaseService;

    /**
     * 分页接口
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/pageList", produces = "text/html;charset=UTF-8")
    public R pageList(HttpServletRequest request, ExtTestCaseQueryModel model) {
        return R.ok(extTestCaseService.getPageResult(model));
    }

    @RequestMapping(value = "/stopAll", produces = "text/html;charset=UTF-8")
    public R stopAll(HttpServletRequest request) {
        extTestCaseService.stopAllExecuting();
        return R.ok();
    }

    /**
     * 创建
     *
     * @param request
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/create", produces = "text/html;charset=UTF-8")
    public R create(HttpServletRequest request, Integer id) throws Exception {
        return extTestCaseService.create(id);
    }

    /**
     * 取消
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/cancel", produces = "text/html;charset=UTF-8")
    public R cancel(HttpServletRequest request, Integer id) {
        return extTestCaseService.cancel(id);
    }

    /**
     * 删除
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove", produces = "text/html;charset=UTF-8")
    public R remove(HttpServletRequest request, Integer id) {
        return extTestCaseService.remove(id);
    }

    @RequestMapping(value = "/previewPdf", produces = "text/html;charset=UTF-8")
    public void previewPdf(HttpServletResponse response, @RequestParam Integer id) {
        extTestCaseService.previewPdf(response, id);
    }

    /**
     * 下载zip报告前的校验
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/checkBeforeDownReport", produces = "text/html;charset=UTF-8")
    public R checkBeforeDownReport(HttpServletRequest request, @RequestParam Integer id) {
        return extTestCaseService.checkBeforeDownReport(id);
    }

    @RequestMapping(value = "/downloadReport", produces = "text/html;charset=UTF-8")
    public void downloadReport(HttpServletResponse response, @RequestParam Integer id) {
        extTestCaseService.downloadReport(response, id);
    }

    /**
     * 预览PDF前的校验
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/checkBeforePreviewPdf", produces = "text/html;charset=UTF-8")
    public R checkBeforePreviewPdf(HttpServletRequest request, @RequestParam Integer id) {
        return extTestCaseService.checkBeforePreviewPdf(id);
    }

    /**
     * 保存
     *
     * @param request
     * @param jsonStr
     * @return
     */
    @RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
    public R save(HttpServletRequest request, @RequestParam String jsonStr) {
        List<ExtTestCaseInfoEntity> list = JsonUtil.json2ObjectLists(jsonStr, ExtTestCaseInfoEntity.class);
        extTestCaseService.save(list);
        return R.ok();
    }

    /**
     * 获取执行日志
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/getLog", produces = "text/html;charset=UTF-8")
    public R getLog(HttpServletRequest request, @RequestParam Integer id) {
        return extTestCaseService.getLog(id);
    }


    /**
     * 上传测试用例jmx文件
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/uploadJmxFile", produces = "text/html;charset=UTF-8")
    public R uploadJmxFile(HttpServletRequest request) {
        List<JmxUploadResult> list = extTestCaseService.uploadJmxFile(request);
        return R.ok(list);
    }

    /**
     * 获取详情信息
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/getDetailInfo", produces = "text/html;charset=UTF-8")
    public R getDetailInfo(HttpServletRequest request, @RequestParam Integer id) {
        ExtTestCaseInfoEntity entity = extTestCaseService.getDetailInfo(id);
        return R.ok(entity);
    }

    /**
     * 验证Jmeter是否安装,可使用
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/checkJmeterReady", produces = "text/html;charset=UTF-8")
    public R checkJmeterReady(HttpServletRequest request) throws Exception {
        String info = extTestCaseService.checkJmeterReady();
        return R.ok(info);
    }

    /**
     * 验证PhantomJs是否安装,可使用
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/checkPhantomJsReady", produces = "text/html;charset=UTF-8")
    public R checkPhantomJsReady(HttpServletRequest request) throws Exception {
        String info = extTestCaseService.checkPhantomJsReady();
        return R.ok(info);
    }

}
